//
//  ActionClassView.m
//  A段项目,漫画
//
//  Created by dlios on 15-4-30.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ActionClassView.h"
#import "MJRefresh.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface ActionClassView ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *actionSecondView;
@property(nonatomic,retain)NSMutableArray *arr;
@property(nonatomic,assign)NSInteger page;
//动画
@property(nonatomic, retain)UIView *witeView;
@property(nonatomic, retain)NSMutableArray *witeImageArr;
@property(nonatomic, retain)UIImageView *witeImageView;
@property(nonatomic, retain)UIProgressView *myProgress;
@property(nonatomic, retain)UILabel *myLabel;
@end

@implementation ActionClassView
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray array];
    }
    return self;
}
- (void)createWiteImage{
    self.witeImageArr = [NSMutableArray array];
    NSArray *names = @[@"735d4677ec7cdd522281cfbddd29ac71－1（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－2（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－3（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－4（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－5（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－6（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－7（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－8（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－9（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－10（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－11（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－12（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－13（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－14（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－15（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－16（被拖移）.tiff"];
    for (NSString *name in names) {
        UIImage *image = [UIImage imageNamed:name];
        [self.witeImageArr addObject:image];
    }
    self.witeImageView.animationImages = self.witeImageArr;
    //设置播放时间
    self.witeImageView.animationDuration = 0.5;
}
- (void)createAvtivity{
    self.witeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.witeView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.witeView];
    [self.witeView release];
    
    CGFloat bili = 222.0 / 271;
    self.witeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 1.5, WIDTH / 1.5 * bili)];
    self.witeImageView.center = CGPointMake(WIDTH / 2, HEIGHT / 3);
    [self.witeView addSubview:self.witeImageView];
    [self.witeImageView release];
    
    self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 1.5, 30)];
    self.myLabel.center = CGPointMake(WIDTH / 2, HEIGHT / 3 + WIDTH / 1.5 * bili / 2 + 10);
    self.myLabel.text = @"loading..........";
    [self.witeView addSubview:self.myLabel];
    self.myLabel.textAlignment = NSTextAlignmentCenter;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor orangeColor];
    [self createAvtivity];
    [self createWiteImage];
    [self crearView];
    
    [self PleaseData];
    
    [self addHearder];
    
    [self addFooter];
    [self.view bringSubviewToFront:self.witeView];
    //播放动画
    [self.witeImageView startAnimating];
}

//数据
-(void)PleaseData
{
    self.page++;
    NSString *str = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Api&ios=23&pad=0&a=listByType&id=%@&page=%ld&limit=30&order=1",self.video_id,self.page];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableDictionary *listArr = [dic objectForKey:@"list"];
        for (NSMutableDictionary *tempDic in listArr) {
            ActionClassSecond *act = [[ActionClassSecond alloc]init];
            [act setValuesForKeysWithDictionary:tempDic];
            [self.arr addObject:act];
            [act release];
        }
        [self.actionSecondView reloadData];
        NSLog(@"请求成功");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"失败");
        
    }];
}


//视图
-(void)crearView
{
    self.actionSecondView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    self.actionSecondView.backgroundColor = [UIColor whiteColor];
    self.actionSecondView.rowHeight = 121;
    [self.view addSubview:self.actionSecondView];
    [_actionSecondView release];
    
    self.actionSecondView.delegate = self;
    self.actionSecondView.dataSource = self;
}


//协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse = @"reuse";
    ActionClassSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[ActionClassSecondCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
    }
    ActionClassSecond *act = self.arr[indexPath.row];
    NSURL *url = [NSURL URLWithString:act.cover];
    [cell.picImageView sd_setImageWithURL:url];
    cell.namelable.text = act.name;
    NSString *str1 = [NSString stringWithFormat:@"更新至%@集",act.cur_num];
    cell.cur_numlabel.text = str1;
    NSString *str = [NSString stringWithFormat:@"%@分",act.score];
    cell.scorelabel.text = str;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 3) {
        NSLog(@"结束动画");
        [self.witeImageView stopAnimating];
        [self.view bringSubviewToFront:self.actionSecondView];
    }
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActionClassSecond *act = self.arr[indexPath.row];
    NSLog(@"ID  %@",act.video_id);
    ActionDViewController *advc = [[ActionDViewController alloc]init];
    advc.video_id = act.video_id;
    [self.navigationController pushViewController:advc animated:YES];
    [advc release];
    
}

-(void)addHearder
{
    [self.actionSecondView addHeaderWithCallback:^{
        self.page = 0;
        [self.arr removeAllObjects];
        [self PleaseData];
        [self.actionSecondView reloadData];
        [self.actionSecondView headerEndRefreshing];
    }];
    [self.actionSecondView headerBeginRefreshing];
}

-(void)addFooter
{
    [self.actionSecondView addFooterWithCallback:^{
        [self PleaseData];
        [self.actionSecondView reloadData];
        [self.actionSecondView footerEndRefreshing];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
